package com.junt.library.view;

import android.database.ContentObserver;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;

import com.junt.library.MediaSelector;
import com.junt.library.R;
import com.junt.library.adapter.MediaAdapter;
import com.junt.library.core.QueryTask;
import com.junt.library.core.QueryType;
import com.junt.library.interfaces.SimpleQueryListener;
import com.junt.library.model.Media;

import java.util.List;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * 将数据查询统一封装
 */
public abstract class BaseMediaActivity extends AppCompatActivity {
    protected QueryTask queryTask;
    private ContentObserver contentObserver;
    private Handler handler;

    abstract int getLayoutResId();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        contentObserver = new ContentObserver(null) {
            @Override
            public boolean deliverSelfNotifications() {
                return true;
            }

            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);
                Log.i("BaseMediaActivity", "onChange: selfChange=" + selfChange);
            }

            @Override
            public void onChange(boolean selfChange, Uri uri) {
                super.onChange(selfChange, uri);
                Log.i("BaseMediaActivity", "onChange: uri=" + uri.toString());
            }
        };
        registerContentObserver();
        init();
        initData();
    }

    protected void registerContentObserver() {
        Uri externalUri;
        QueryType queryType = MediaSelector.getInstance().getQueryType();
        if (queryType == QueryType.VIDEO) {
            externalUri = Uri.parse("content://" + MediaStore.AUTHORITY + "/external/video");
        } else if (queryType == QueryType.AUDIO) {
            externalUri = Uri.parse("content://" + MediaStore.AUTHORITY + "/external/audio");
        } else {
            externalUri = Uri.parse("content://" + MediaStore.AUTHORITY + "/external/images");
        }
        getContentResolver().registerContentObserver(externalUri, true, contentObserver);
    }

    protected abstract void init();

    protected abstract void onDataReady(List<Media> medias);

    protected void onDataNext(Media media) {
        Log.i(getClass().getSimpleName(), "onDataNext: "+media.toString());
    }

    protected void initData() {
        queryTask = new QueryTask(getApplication(), new SimpleQueryListener() {
            @Override
            public void onStart() {
                super.onStart();
                Log.i("MediaSelector", "start scan");
            }

            @Override
            public void onNext(Media media) {
                super.onNext(media);
                onDataNext(media);
            }

            @Override
            public void onComplete(List<Media> medias) {
                Log.i("MediaSelector", "scan completely");
                onDataReady(medias);
            }

            @Override
            public void onError(Exception e) {
                super.onError(e);
                Log.e("MediaSelector", "onError: " + e.getMessage());
            }
        });
        queryTask.execute();
    }

    @Override
    public void onBackPressed() {
        MediaSelector.getInstance().setNoneResult(this);
    }

    @Override
    protected void onStop() {
        queryTask.cancel(true);
        getContentResolver().unregisterContentObserver(contentObserver);
        super.onStop();
    }
}
